package com.hang.gymmanager.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hang.gymmanager.common.BasePageReq;
import com.hang.gymmanager.common.Response;
import com.hang.gymmanager.entity.CommodityEntity;
import com.hang.gymmanager.req.*;
import com.hang.gymmanager.res.CommodityRes;
import com.hang.gymmanager.res.UserCommodityPageRes;
import com.hang.gymmanager.service.CommodityService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;


/**
 * 商品控制器
 */
@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/commodity")
@CrossOrigin
public class CommodityController {

    private final CommodityService commodityService;

    /**
     * 商品列表
     * @param req
     * @return
     */
    @PostMapping("/get")
    public Response<Page<CommodityRes>> getAllCommodity(@RequestBody PageCommodityReq req){
        Page<CommodityRes> allCommodity = commodityService.getAllCommodity(req);
        return Response.success(allCommodity);
    }

    /**
     * 添加商品
     * @param req
     * @return
     */
    @PostMapping("/add")
    public Response<Integer> addCommodity(@RequestBody AddCommodityReq req){
        Integer id = commodityService.addCommodity(req);
        return Response.success(id);
    }

    /**
     * 添加商品图片
     * @param file
     * @param commodityId
     * @return
     */
    @PostMapping("/uploadPic")
    public Response uploadPic(@RequestParam("file") MultipartFile file,
                              @RequestParam("commodityId") Integer commodityId){
        commodityService.uploadPic(file,commodityId);
        return Response.success();
    }

    /**
     * 删除商品
     * @param req
     * @return
     */
    @PostMapping("/delete")
    public Response deleteCommodity(@RequestBody DeleteCommodityReq req){
        commodityService.deleteCommodity(req);
        return Response.success();
    }

    /**
     * 更改商品
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response updateCommodity(@RequestBody UpdateCommodityReq req){
        commodityService.updateCommodity(req);
        return Response.success();
    }

    /**
     * 查询商品详情
     * @param id
     * @return
     */
    @GetMapping("/detail/{id}")
    public Response<CommodityEntity> commodityDetail(@PathVariable Integer id){
        CommodityEntity entity = commodityService.commodityDetail(id);
        return Response.success(entity);
    }

    /**
     * 用户查询
     * @param req
     * @return
     */
    @PostMapping("/userCommodityPage")
    public Response<Page<UserCommodityPageRes>> userCommodityPage(@RequestBody userCommodityPageReq req){
        Page<UserCommodityPageRes> res = commodityService.userCommodityPage(req);
        return Response.success(res);
    }


    /**
     * 用户购买商品
     * @param req
     * @return
     */
    @PostMapping("/buy")
    public Response<String> buy(@RequestBody List<BuyCommodityReq> req){
        commodityService.buy(req);
        return Response.success("购买成功");
    }

}
